package com.thd.springboottest.camunda.controller;

import com.thd.springboottest.camunda.service.WorkFlowService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * com.thd.springboottest.camunda.controller.CamundaController
 *
 * @author: wanglei62
 * @DATE: 2022/5/26 17:01
 **/
@Controller
@RequestMapping("/camunda/processVariable")
public class Camunda03processVariable {
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private WorkFlowService workFlowService;
    /**
     * 部署
     */
    // url : http://127.0.0.1:8899/thd/camunda/processVariable/deploy
    @RequestMapping(value="/deploy",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deploy(){
        String deployId = this.workFlowService.deploy("processes/03processVariable.bpmn");
        System.out.println("deployId:" + deployId);
        return ResponseEntity.ok("SUCCESS");
    }

    /**
     * 开启流程
     */
    // url : http://127.0.0.1:8899/thd/camunda/processVariable/startProcess/[businessKey:processVariable001]
    @RequestMapping(value="/startProcess/{businessKey}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> startProcess(@PathVariable String businessKey){
        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("processVariable",businessKey);
       System.out.println("流程实例ID:" + pi.getProcessInstanceId());
        return ResponseEntity.ok("SUCCESS");
    }

    /*
    设置流程实例的流程变量: http://127.0.0.1:8899/thd/camunda/addVariableForProcessInstance/[processInstanceId]
    {
        "candidates":["zhangsan","lisi","wangwu"],
        "processVar":"processVar-01",
        "a":1,
        "b":0
    }
    a和b的值可以是0或1,控制流程扭转方向
     */

    /*
    查询待办: http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/processVariable001
    获取代办ID
     */

    /*
    完成代办：http://127.0.0.1:8899/thd/camunda/completeTask/[taskId]
     */

    /*
    查询待办: http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/processVariable001
    获取代办ID和executionId (根据分支不同可能会有多个待办)
     */

    // ========================== 以下是对流程变量的操作 =========================== //

    /*
    查询流程实例的流程变量
    http://127.0.0.1:8899/thd/camunda/queryVariableForProcessInstance/7a6d3e40-dfe4-11ec-ab17-e86a64fc0114
     */

    /*
    设置待办的本地流程变量
    http://127.0.0.1:8899/thd/camunda/addVariableForTaskLocal/22471a50-dfe6-11ec-ab17-e86a64fc0114
    {
    "taskLocalVar":"taskLocalVar01"
    }
     */

    /*
    设置待办的流程变量
    http://127.0.0.1:8899/thd/camunda/addVariableForTask/22471a50-dfe6-11ec-ab17-e86a64fc0114
    {
    "taskVar":"taskVar01"
    }
     */

    /*
    设置执行实例的流程变量
    http://127.0.0.1:8899/thd/camunda/addVariableForExecution/2246cc2d-dfe6-11ec-ab17-e86a64fc0114
    {
    "executionVar":"executionVar01"
    }
     */

    /*
    设置执行实例的本地流程变量
    http://127.0.0.1:8899/thd/camunda/addVariableForExecutionLocal/2246cc2d-dfe6-11ec-ab17-e86a64fc0114
    {
    "executionLocalVar":"executionLocalVar01"
    }
     */


    /*
    查询流程实例的流程变量
    http://127.0.0.1:8899/thd/camunda/queryVariableForProcessInstance/7a6d3e40-dfe4-11ec-ab17-e86a64fc0114
    {
    "candidates": [
        "zhangsan",
        "lisi",
        "wangwu"
    ],
    "a": 1,
    "processVar": "processVar-01",
    "b": 0,
    "executionVar": "executionVar01",
    "taskVar": "taskVar01"
    }

    在流程实例上可以看到流程实例以及执行实例和待办实例的流程变量，而看不到执行实例和待办实例的本地流程变量
     */


    /*
    查询执行实例的流程变量
    http://127.0.0.1:8899/thd/camunda/queryVariableForExecution/2246cc2d-dfe6-11ec-ab17-e86a64fc0114
    {
    "candidates": [
        "zhangsan",
        "lisi",
        "wangwu"
    ],
    "a": 1,
    "processVar": "processVar-01",
    "b": 0,
    "executionLocalVar": "executionLocalVar01",
    "executionVar": "executionVar01",
    "taskVar": "taskVar01"
    }

    可以看到流程实例的流程变量 以及 执行实例本身的流程变量和本地变量 以及 待办的流程变量(待办的本地流程变量不可见)
     */

    /*
    查询执行实例的本地流程变量
    http://127.0.0.1:8899/thd/camunda/queryVariableForExecutionLocal/2246cc2d-dfe6-11ec-ab17-e86a64fc0114
    {
    "executionLocalVar": "executionLocalVar01"
    }

    只能看到执行实例的本地流程变量
     */


    /*
    查询待办的流程变量
    http://127.0.0.1:8899/thd/camunda/queryVariableForTask/22471a50-dfe6-11ec-ab17-e86a64fc0114
    {
    "candidates": [
        "zhangsan",
        "lisi",
        "wangwu"
    ],
    "a": 1,
    "processVar": "processVar-01",
    "b": 0,
    "taskLocalVar": "taskLocalVar01",
    "executionLocalVar": "executionLocalVar01",
    "executionVar": "executionVar01",
    "taskVar": "taskVar01"
    }
    可以看到流程实例的流程变量以及执行实例的流程变量和本地变量 以及自身待办的流程变量(本地流程变量不可见) 以及自身的流程变量和本地流程变量
     */

    /*
    查询待办的本地流程变量
    http://127.0.0.1:8899/thd/camunda/queryVariableForTaskLocal/22471a50-dfe6-11ec-ab17-e86a64fc0114
    {
        "taskLocalVar": "taskLocalVar01"
    }
    只能看到待办自身的本地流程变量
     */



    /*
    设置流程变量 - 候选人

     */


}
